package org.ljy.lotterysystem.service.activitystatus.operater;

import org.ljy.lotterysystem.dao.dataobject.ActivityDO;
import org.ljy.lotterysystem.dao.mapper.ActivityMapper;
import org.ljy.lotterysystem.dao.mapper.ActivityPrizeMapper;
import org.ljy.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.ljy.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.ljy.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{


    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;


    @Override
    public Integer sqquance() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO statusConvertDTO) {
        Long activityId = statusConvertDTO.getActivityId();
        ActivityStatusEnum targetStatus = statusConvertDTO.getTargetActivityStatus();
        if(activityId == null
                || targetStatus == null) {
            return false; //不需要扭转
        }
        ActivityDO activityDO = activityMapper.selectByActivityId(activityId);
        if(activityDO == null) {
            return false;
        }
        //查看当前状态和传入的状态是否一致
        if(targetStatus.name().equalsIgnoreCase(activityDO.getStatus())) {
            return false;
        }
        //查看奖品是否已经抽完
        int count = activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if(count > 0) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO statusConvertDTO) {
        try {
            activityMapper.updateStatus(statusConvertDTO.getActivityId(),
                    statusConvertDTO.getTargetActivityStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
